package com.video.runlog;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class ElasticSearchConfig {

    @Value("http")
    private String schemal;

    @Value("${spring.elasticsearch.hosts}")
    private String hosts;

    @Bean
    public RestClientBuilder getRestClient(){

        if (hosts != null) {
            List<HttpHost> hostsList = new ArrayList<>();
            String[] hostsArray = hosts.split(",");
            for (String host : hostsArray) {
                String[] uri = host.split(":");
                HttpHost httpHost = new HttpHost(uri[0], Integer.parseInt(uri[1]), schemal);
                hostsList.add(httpHost);
            }

            RestClientBuilder builder = RestClient.builder(hostsList.toArray(new HttpHost[0]));
            return builder;
        }

        return null;
    }
}
